package br.com.ime.usp.cmep;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.AbstractAction;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;

public class JanusActivity extends Activity{
	WebView webview;
	final JanusActivity activity = this;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.janus);
		
		// action bar
        final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
        actionBar.setTitle(getString(R.string.janus_titulo));
        actionBar.setHomeAction(new HomeAction(this));        
        actionBar.addAction(new BackAction());
		
        // janus
        webview = (WebView) findViewById(R.id.janus_webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setSupportZoom(true);
        webview.getSettings().setBuiltInZoomControls(true);
        webview.loadUrl("https://uspdigital.usp.br/");
        webview.setWebViewClient(new HelloWebViewClient());
        webview.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                actionBar.setTitle(getString(R.string.janus_carregando).replaceAll("\\{p\\}", progress + ""));
                if(progress == 100)
                	actionBar.setTitle(getString(R.string.janus_titulo));
            }
        });
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
	        webview.goBack();
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	
	
	
	
	
	
	
	
	// ---------- action bar ---------
	// volta para a tela anterior
    private class BackAction extends AbstractAction {
        public BackAction() {
            super(R.drawable.ic_action_back);
        }
        public void performAction(View view) {
        	if(activity.webview.canGoBack())
        		activity.webview.goBack();
        	else
        		activity.finish();
        }
    }
    
    // volta pra home
    private class HomeAction extends AbstractAction {
    	private Activity act;
        public HomeAction(Activity a) {
            super(R.drawable.ic_launcher); act = a;
        }
        public void performAction(View view) {
        	Intent i = new Intent(act, CMEPActivity.class);
        	act.startActivity(i);
        	act.finish();
        }
    }
    
    
    // ------- WebView ----------
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}
